package com.upup.train_management.controller;

import com.upup.train_management.entity.Route;
import com.upup.train_management.entity.RouteStation;
import com.upup.train_management.entity.Station;
import com.upup.train_management.service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/routes")
public class RouteController {

    private final RouteService routeService;

    @Autowired
    public RouteController(RouteService routeService) {
        this.routeService = routeService;
    }

    @GetMapping
    public ResponseEntity<List<Route>> getAllRoutes() {
        return ResponseEntity.ok(routeService.getAllRoutes());
    }

    @GetMapping("/{id}")
    public ResponseEntity<Route> getRouteById(@PathVariable Long id) {
        Route route = routeService.getRouteById(id);
        if (route != null) {
            return ResponseEntity.ok(route);
        }
        return ResponseEntity.notFound().build();
    }

    @GetMapping("/{id}/stations")
    public ResponseEntity<List<Station>> getRouteStations(@PathVariable Long id) {
        List<Station> stations = routeService.getStationsInRoute(id);
        return ResponseEntity.ok(stations);
    }

    @GetMapping("/start-station/{stationId}")
    public ResponseEntity<List<Route>> getRoutesByStartStation(@PathVariable Long stationId) {
        return ResponseEntity.ok(routeService.getRoutesByStartStation(stationId));
    }

    @GetMapping("/end-station/{stationId}")
    public ResponseEntity<List<Route>> getRoutesByEndStation(@PathVariable Long stationId) {
        return ResponseEntity.ok(routeService.getRoutesByEndStation(stationId));
    }

    @GetMapping("/search")
    public ResponseEntity<List<Route>> searchRoutes(
            @RequestParam Long startStationId,
            @RequestParam Long endStationId) {
        return ResponseEntity.ok(routeService.searchRoutes(startStationId, endStationId));
    }

    @GetMapping("/{id}/distance")
    public ResponseEntity<Double> getRouteDistance(@PathVariable Long id) {
        Double distance = routeService.calculateRouteDistance(id);
        return ResponseEntity.ok(distance);
    }

    @PostMapping
    public ResponseEntity<Route> createRoute(@RequestBody Route route) {
        return ResponseEntity.ok(routeService.saveRoute(route));
    }

    @PostMapping("/{routeId}/stations")
    public ResponseEntity<Void> addStationToRoute(
            @PathVariable Long routeId,
            @RequestBody RouteStation routeStation) {
        routeStation.setRouteId(routeId);
        routeService.addStationToRoute(routeStation);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/{id}")
    public ResponseEntity<Route> updateRoute(@PathVariable Long id, @RequestBody Route route) {
        Route existingRoute = routeService.getRouteById(id);
        if (existingRoute == null) {
            return ResponseEntity.notFound().build();
        }
        route.setId(id);
        return ResponseEntity.ok(routeService.saveRoute(route));
    }

    @PutMapping("/{routeId}/stations/{stationId}/order")
    public ResponseEntity<Void> updateStationOrder(
            @PathVariable Long routeId,
            @PathVariable Long stationId,
            @RequestParam Integer newOrder) {
        routeService.updateRouteStationOrder(routeId, stationId, newOrder);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteRoute(@PathVariable Long id) {
        Route existingRoute = routeService.getRouteById(id);
        if (existingRoute == null) {
            return ResponseEntity.notFound().build();
        }
        routeService.deleteRoute(id);
        return ResponseEntity.noContent().build();
    }

    @DeleteMapping("/{routeId}/stations/{stationId}")
    public ResponseEntity<Void> removeStationFromRoute(
            @PathVariable Long routeId,
            @PathVariable Long stationId) {
        routeService.removeStationFromRoute(routeId, stationId);
        return ResponseEntity.noContent().build();
    }
    @GetMapping("/search/city")
    public ResponseEntity<List<Route>> searchRoutesByCity(
            @RequestParam String startCity,
            @RequestParam String endCity) {
        return ResponseEntity.ok(routeService.searchRoutesByCity(startCity, endCity));
    }

    @GetMapping("/most-popular")
    public ResponseEntity<List<Route>> getMostPopularRoutes(@RequestParam(defaultValue = "10") Integer limit) {
        return ResponseEntity.ok(routeService.getMostPopularRoutes(limit));
    }
}